package com.hontech.libratest.activity.fragment.sub

import android.view.View
import android.widget.Button
import android.widget.EditText
import com.hontech.libratest.R
import com.hontech.libratest.activity.window.showProg
import com.hontech.libratest.conn.delivery
import com.hontech.libratest.conn.reset
import com.hontech.libratest.utils.onClick
import com.hontech.libratest.utils.toInt

class DeliverySub(view: View) {

    private val etRow = view.findViewById<EditText>(R.id.id_del_et_row)
    private val etCol = view.findViewById<EditText>(R.id.id_del_et_col)
    private val etSize = view.findViewById<EditText>(R.id.id_del_et_size)
    private val etTimeout = view.findViewById<EditText>(R.id.id_del_et_timeout)
    private val btnDelivery = view.findViewById<Button>(R.id.id_del_btn_delivery)
    private val btnReset = view.findViewById<Button>(R.id.id_del_btn_reset)

    init {
        btnDelivery.onClick(::onClickDelivery)
        btnReset.onClick(::onClickReset)
    }

    private suspend fun onClickDelivery() {
        val row = etRow.toInt()
        val col = etCol.toInt()
        val size = etSize.toInt()
        val timeout = etTimeout.toInt()

        val w = showProg(btnDelivery, "出货${row}-$col")
        try {
            delivery(row, col, size, timeout)
            w.success("出货成功")
        } catch (e: Exception) {
            e.printStackTrace()
            w.error(e.message!!)
        }
    }

    private suspend fun onClickReset() {
        val w = showProg(btnDelivery, "设备复位")
        try {
            reset()
            w.success("复位成功")
        } catch (e: Exception) {
            e.printStackTrace()
            w.error(e.message!!)
        }
    }

}